//
//  LYQMineViewController.m
//  ZCProject
//
//  Created by Zheng on 16/11/9.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQMineViewController.h"
#import "LYQMineSettingsViewController.h"
#import "LYQMineIntegralViewController.h"
#import "LYQMineLookedMeViewController.h"
#import "LYQNameCardEditViewController.h"

#import "LYQMineCardCell.h"
#import "LYQMineSoftCell.h"
#import "LYQMineDefaultCell.h"
#import "LYQMineSoftFunctionCell.h"

#define kLYQMineCardCell_ID @"kLYQMineCardCell_ID"
#define kLYQMineSoftCell_ID @"kLYQMineSoftCell_ID"
#define kLYQMineDefaultCell_ID @"kLYQMineDefaultCell_ID"
#define kLYQMineSoftFunctionCell_ID @"kLYQMineSoftFunctionCell_ID"

@interface LYQMineViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSArray *titleNamesArr;
@property (nonatomic, strong) NSArray *imageNamesArr;
@property (nonatomic, strong) UIView *cardFooterView;

@end

@implementation LYQMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我";
    
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    [self.listTableView registerClass:[LYQMineCardCell class] forCellReuseIdentifier:kLYQMineCardCell_ID];
    [self.listTableView registerClass:[LYQMineSoftCell class] forCellReuseIdentifier:kLYQMineSoftCell_ID];
    [self.listTableView registerClass:[LYQMineDefaultCell class] forCellReuseIdentifier:kLYQMineDefaultCell_ID];
    [self.listTableView registerClass:[LYQMineSoftFunctionCell class] forCellReuseIdentifier:kLYQMineSoftFunctionCell_ID];
    
    _titleNamesArr = @[@"谁看过我",@"我的认证",@"收藏",@"设置"];
    _imageNamesArr = @[@"skgwicon",@"renzhengicon",@"shoucangicon",@"shezhiicon"];
    
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 2;
    } else {
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        LYQMineCardCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQMineCardCell_ID forIndexPath:indexPath];
        return cell;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            LYQMineSoftCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQMineSoftCell_ID forIndexPath:indexPath];
            return cell;
        } else {
            LYQMineSoftFunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQMineSoftFunctionCell_ID forIndexPath:indexPath];
            return cell;
        }
    } else if (indexPath.section == 2) {
        LYQMineDefaultCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQMineDefaultCell_ID forIndexPath:indexPath];
        cell.titleLbl.text = _titleNamesArr[indexPath.row];
        cell.iconImgv.image = [UIImage imageNamed:_imageNamesArr[indexPath.row]];
        return cell;
    }
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    return cell;
}

#pragma mark - tableview delegate
#pragma mark 头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.0001f;
    } else {
        return kWScale(10);
    }
}

#pragma mark 尾视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return kWScale(45);
    } else {
        return 0.0001f;
    }
}

#pragma mark 自定义尾视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == 0) {
        if (!_cardFooterView) {
            
            NSArray *imageNamesArr = @[@"m_bjmpicon",@"m_jficon"];
            NSArray *titleNamesArr = @[@"编辑名片",@"积分"];
            
            _cardFooterView = [[UIView alloc] init];
            _cardFooterView.backgroundColor = kLYQSystemGray;
            
            for (int i = 0; i < 2; i++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.backgroundColor = [UIColor whiteColor];
                [btn setImage:[UIImage imageNamed:imageNamesArr[i]] forState:UIControlStateNormal];
                btn.frame = CGRectMake(((kMS_Width+0.25)/2+0.25)*i, 0, (kMS_Width-0.25)/2, kWScale(45)-0.5);
                [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
                [btn setTitle:titleNamesArr[i] forState:UIControlStateNormal];
                [btn setTitleEdgeInsets:UIEdgeInsetsMake(-2, 5, 0, 0)];
                [btn setTitleColor:kHexadecimal(@"666666") forState:UIControlStateNormal];
                btn.titleLabel.font = kWFont(15);
                [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
                [_cardFooterView addSubview:btn];
            }
        }
        return _cardFooterView;
    }
    
    return nil;
}

#pragma mark 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return kWScale(100);
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return kWScale(50);
        } else {
            return kWScale(80);
        }
    } else {
        return kWScale(55);
    }
}

#pragma mark 设置分割线偏移量
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
    } else {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, kWScale(10), 0, 0)];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, kWScale(10), 0, 0)];
        }
    }
}

#pragma mark 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        NSLog(@"个人信息");
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
#pragma mark 跳转谁看过我界面
            LYQMineLookedMeViewController *mlmVC = [[LYQMineLookedMeViewController alloc] init];
            mlmVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:mlmVC animated:YES];
        } else if (indexPath.row == 1) {
            NSLog(@"我的认证");
        } else if (indexPath.row == 2) {
            NSLog(@"收藏");
        } else {
#pragma mark 跳转设置界面
            LYQMineSettingsViewController *msVC = [[LYQMineSettingsViewController alloc] init];
            msVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:msVC animated:YES];
        }
    }
}

#pragma mark - button点击事件
- (void)btnAction:(UIButton *)sender {
    
    if ([sender.titleLabel.text isEqualToString:@"编辑名片"]) {
#pragma mark 跳转编辑名片页面
        LYQNameCardEditViewController *nceVC = [[LYQNameCardEditViewController alloc] init];
        nceVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:nceVC animated:YES];
    } else {
#pragma mark 跳转积分页面
        LYQMineIntegralViewController *miVC = [[LYQMineIntegralViewController alloc] init];
        miVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:miVC animated:YES];
    }
}


#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
